package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.JingXuanBookTabAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.BookTabBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.mine.ShopCartActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 精选
 */
public class JingXuanActivity extends BaseActivity {
    public static final int REQUEST_VIDEO_CODE = 8175;
    public static final int REQUEST_CART_CODE = 8174;
    @BindView(R.id.tv_count)
    TextView tvShopCount;//购物车数量
    @BindView(R.id.lvHorizontal)
    RecyclerView lvHorizontalView;
    private JingXuanBookTabAdapter mVideoTabAdapter;
    private List<BookTabBean> mTabData;
    private HashMap<Integer, JingXuanVideoFragment> mVideoFragmentList;
    private int mShopNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jing_xuan);
        ButterKnife.bind(this);
        setTitle("范儿视频", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_video_detail_cart);
        RelativeLayout rlTop = findViewById(R.id.rl_top);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT, rlTop, R.color.gray_bar);

        initView();
        getVideoClassify();
    }

    private void initView() {
        mVideoFragmentList = new HashMap<>();
        lvHorizontalView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        mTabData = new ArrayList<>();
        mVideoTabAdapter = new JingXuanBookTabAdapter(R.layout.item_jingxuan_books_tab, mTabData);
        mVideoTabAdapter.setType(1);
        lvHorizontalView.setAdapter(mVideoTabAdapter);

        lvHorizontalView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                mVideoTabAdapter.setSelectPosition(position);
                setSelection(position);
            }
        });

        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            tvShopCount.setVisibility(View.GONE);
        }
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }
        Intent intent = new Intent(this, ShopCartActivity.class);
        startActivityForResult(intent, REQUEST_CART_CODE);
    }

    private void setSelection(int index) {
        if (index<0 || index >= mTabData.size()) {
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        JingXuanVideoFragment fragment = null;
        if (mVideoFragmentList.containsKey(index)) {
            fragment = mVideoFragmentList.get(index);
        }
        hideFragment(fragmentTransaction);
        if (fragment == null) {
            fragment = JingXuanVideoFragment.newInstance(index, mTabData.get(index).getId(), null);

            fragmentTransaction.add(R.id.content_layout, fragment, "tab" + index);
            mVideoFragmentList.put(index, fragment);
        } else {
            fragmentTransaction.show(fragment);
        }
        fragmentTransaction.commit();
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        for (int i = 0; i < mVideoFragmentList.size(); i++) {
            Fragment fragment = mVideoFragmentList.get(i);
            if (fragment != null) {
                transaction.hide(fragment);
            }

        }
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        login();
    }

    private void getVideoClassify() {
        loadWaitDialog();
        HttpMethod.videoClassify(this, this);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_VIDEO_CLASSIFY) {
            ResponseList<BookTabBean> responseBean = GsonUtil.fromJsonList(data, BookTabBean.class);
            if (responseBean != null && responseBean.getClassify() != null) {
                mTabData.clear();
                mTabData.addAll(responseBean.getClassify());
                mVideoTabAdapter.notifyDataSetChanged();
                setSelection(0);
            }
        }
    }

    public void setShopCount(int shopCount) {
        mShopNum = shopCount;
        tvShopCount.setText(String.valueOf(mShopNum));
    }

    /**
     * @param type 1减，2加
     */
    public void changeShopCount(int type) {
        if (type == 1) {
            mShopNum--;
        } else if (type == 2) {
            mShopNum++;
        }
        tvShopCount.setText(String.valueOf(mShopNum));
    }
}
